﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.Script.Serialization;
using System.Linq;

using MvcCms.Data;
using MvcCms.Service.Filters;
using MvcCms.Service.Code.Email;
using MvcCms.Service;
using MvcCms.Store.Web.ViewModels;
using MvcCms.Data.Entities;
using MvcCms.Service.Code.Encryption;
using MvcCms.Service.Code;

using MvcCms.Store.Service;
using MvcCms.Service.Cache;

namespace MvcCms.Store.Web.Controllers
{
    [RequireSsl(Redirect = true)]  
    public class StoreHomeController : MvcCms.Service.Code.BaseClasses.CMSControllerBase
    {
        #region Constructors
        private readonly IStoreService _storeService;
        private readonly ICacheService _cacheService;
        public StoreHomeController(ICacheService cacheService, IStoreService storeService, IAccountService accountService, IZoneService zoneService, ICmsService cmsService)
            : base(cmsService, zoneService, accountService)
        {
            _storeService = storeService;
            _cacheService = cacheService;
        }
        #endregion

        public ActionResult Index()
        {
            var portal = _cacheService.GetCurrentPortal();
            var homepageCategory = _storeService.ListCategories(portal.PortalID).Where(c=>c.UseForHomePage).FirstOrDefault();            
            var productsToShow = new List<StoreProduct>();

            if (homepageCategory == null)
            {
                productsToShow = _storeService.ListTopSellingActiveProducts(portal.PortalID, 5).ToList();
            }
            else
            {
                productsToShow = _storeService.GetCategory(homepageCategory.Name, portal.PortalID).Products.ToList();
            }

            var model = new DefaultViewModel
            {
                _Portal = portal,
                _Products = productsToShow
            };
            ViewBag.Title = portal.Name;
            return View(model);
        }

    }
}